#coding=utf-8
import re
import requests
import User_Agent_list
import random
import urllib
import sys
import hashlib
import json
import urllib
import chardet
import time
appKey='7ae6374285804341'
miyao="g2hcOe1rEusC8mG9gqurCKdiiNSJ8FWI"
salt=2
def fanyi(word):
    sign = hashlib.new("md5")
    word=word.encode("utf-8")
    sign.update(appKey+word+str(salt)+miyao)
    word = urllib.quote(word)
    #print word
    url = "http://openapi.youdao.com/api?appKey=7ae6374285804341&form=auto&to=auto&salt=%s&sign=%s&q=%s"%(salt,sign.hexdigest(),word)
    #print url
    #url=url+urllib.quote(word)
    #, headers = random.choice(User_Agent_list.get())
    try:
        text=requests.get(url=url,timeout=3)
    except Exception as e:
        print e
        return "404"
    #results=re.findall('\["(.*?)"\]',str(text.content))[0]
    return json.loads(text.content)


def main():
    while True:
        word=raw_input(">>")
        if word=="q":
            sys.exit()
        if word=="":
            continue

        try:
            print fanyi(word)["web"][0]["value"][0]
        except Exception as e:
            print e

            #.decode("utf-8").encode("gbk")

if __name__=="__main__":
    main()
